<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>VUE学习 - 请求和响应拦截器</title>
</head>

<body>


    <!--载入vue.js-->
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

    <div id="Application">


        您的名字是：{{data.name}} <br />
        您的ID是：{{data.id}}

    </div>
    <script>
        const app = Vue.createApp({
            setup() {
                const data = Vue.ref("");

                //请求拦截器
                axios.interceptors.request.use(
                    config => {
                        console.log("发出请求前拦截下")
                        return config
                    },
                    err => {
                        return Promise.reject(err)
                    }
                );

                //响应拦截器
                axios.interceptors.response.use(
                    res => {
                        console.log("响应请求前拦截一下", res)
                        return res
                    },
                    err => {
                        return Promise.reject(err)
                    }

                );

                axios.post('https://rouse.npc.ink/api/data.php')
                    .then(response => {
                        data.value = response.data;
                        console.log("发出请求啦")
                    })
                    //我们前面拦截响应后，无论是请求还是响应的拦截器，他们的err返回的promise都会进入catch中。
                    .catch(err => {
                        alert("错误啦！")
                    });



                return { data };

            },
        })

        app.mount("#Application")
    </script>

</body>

</html>